package org.jeecg.modules.online.graphreport.service.impl;

import org.jeecg.modules.online.graphreport.entity.OnlGraphreportTemplet;
import org.jeecg.modules.online.graphreport.entity.OnlGraphreportTempletItem;
import org.jeecg.modules.online.graphreport.mapper.OnlGraphreportTempletItemMapper;
import org.jeecg.modules.online.graphreport.mapper.OnlGraphreportTempletMapper;
import org.jeecg.modules.online.graphreport.service.IOnlGraphreportTempletService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: Online报表多数据源主表
 * @Author: jeecg-boot
 * @Date:  2019-04-17
 * @Version: V1.0
 */
@Service("onlGraphreportTempletServiceImpl")
public class OnlGraphreportTempletServiceImpl extends ServiceImpl<OnlGraphreportTempletMapper, OnlGraphreportTemplet> implements IOnlGraphreportTempletService {

	@Autowired
	private OnlGraphreportTempletMapper onlGraphreportTempletMapper;
	@Autowired
	private OnlGraphreportTempletItemMapper onlGraphreportTempletItemMapper;
	
	@Override
	@Transactional
	public void saveMain(OnlGraphreportTemplet onlGraphreportTemplet, List<OnlGraphreportTempletItem> onlGraphreportTempletItemList) {
		onlGraphreportTempletMapper.insert(onlGraphreportTemplet);
		for(OnlGraphreportTempletItem entity:onlGraphreportTempletItemList) {
			//外键设置
			entity.setGraphreportTempletId(onlGraphreportTemplet.getId());
			onlGraphreportTempletItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(OnlGraphreportTemplet onlGraphreportTemplet,List<OnlGraphreportTempletItem> onlGraphreportTempletItemList) {
		onlGraphreportTempletMapper.updateById(onlGraphreportTemplet);
		
		//1.先删除子表数据
		onlGraphreportTempletItemMapper.deleteByMainId(onlGraphreportTemplet.getId());
		
		//2.子表数据重新插入
		for(OnlGraphreportTempletItem entity:onlGraphreportTempletItemList) {
			//外键设置
			entity.setGraphreportTempletId(onlGraphreportTemplet.getId());
			onlGraphreportTempletItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		onlGraphreportTempletMapper.deleteById(id);
		onlGraphreportTempletItemMapper.deleteByMainId(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			onlGraphreportTempletMapper.deleteById(id);
			onlGraphreportTempletItemMapper.deleteByMainId(id.toString());
		}
	}
	
}
